package com.ikkong.expert.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ikkong.core.base.BaseController;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ShiroKit;
import com.ikkong.core.toolbox.Record;
import com.ikkong.projects.model.Projects;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.expert.model.Listexpert;
import com.ikkong.expert.service.ListexpertService;
import com.ikkong.expert.service.impl.ListexpertServiceImpl;
import com.ikkong.system.model.Dept;

/**
 * Generated by Blade.
 * 2018-03-29 08:49:57
 */
public class ListexpertController extends UrlPermissController {
	private static String CODE = "listexpert";
	private static String PERFIX = "projects";
	private static String LIST_SOURCE = "Listexpert.mylist";
	private static String BASE_PATH = "/platform/listexpert/";
	private static String ENJOY_PATH="/WEB-INF/view/platform/";
	
	ListexpertService service = new ListexpertServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "listexpert.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "listexpert_add.html");
	}
	public void feed() {
		String id = getPara("id");
		String expert = ShiroKit.getUser().getName().toString();
		String opinion =expert+":"+ getPara("opinion");
		String opinions = service.findById(id).getExpert_opinion();
		if(opinions == null){
			opinions = "";
		}
		opinions = opinions + "<br/>" + opinion;
		Listexpert listexpert = new Listexpert();
		listexpert.setId(Integer.parseInt(id));
		listexpert.setExpert_opinion(opinions);
		listexpert.setStatus(7);
		Boolean temp = service.update(listexpert);
		if (temp) {
			setAttr("href","/listexpert/index");
			setAttr("msg","秒后跳转到待审核项目列表");
			render("/platform/projects/success.html");
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void edit() {
		String id = getPara(0);
		Listexpert listexpert = service.findById(id);
		setAttr("model", JsonKit.toJson(listexpert));
		setAttr("id", id);
		setAttr("code", CODE);
		renderJson(success(SAVE_SUCCESS_MSG));
	}
	public void opinion() {

		String id = getPara("id");
		Listexpert listexpert = service.findById(id);
		setAttr("listexpert",listexpert);
		setAttr("code", CODE);
		render(BASE_PATH + "listexpert_opinion.html");
	}
	public  void tohs(){
		Integer id = getParaToInt("id");
		Integer status = getParaToInt("status");
		Projects pro = Blade.create(Projects.class).findById(id);
		setAttr("pro",pro);

		setAttr("status",status);
			render(BASE_PATH + "tohs_opinion.html");
	}
	public void rehs(){
		Integer id = getParaToInt("id");
		Integer status = getParaToInt("status");
		String opinion = getPara("opinion");
		Projects p = new Projects();
		p.setId(id);
		p.setStatus(status);
		if(status == 8){
			p.setTohs_opinion(opinion);
		}else if(status == 9 || status==97){
			p.setHs_opinion(opinion);
		}
		Blade.create(Projects.class).update(p);
		setAttr("href","/listcom/");
		setAttr("msg","即将跳转至审核列表");
		render(BASE_PATH+"../projects/success.html");
	}
	public void view() {
		String id = getPara(0);
		Listexpert listexpert = service.findById(id);
		Dept dept = Blade.create(Dept.class).findById(listexpert.getDept_id());
		Integer nature = listexpert.getNature();
		String n="";
		if(nature != null){
			if("1".equals(nature.toString())){
				n = "新建";
			}else if("2".equals(nature.toString())){
				n="续建";
			}
		}

		setAttr("projects", listexpert);
		setAttr("dept",dept.getFullname());
		setAttr("nature",n);
		setAttr("id", id);
		setAttr("code", CODE);
		renderTemplate("/WEB-INF/view/platform/listexpert/listexpert_view.html");

	}

	public void list() {
		String userid = ShiroKit.getUser().getId().toString();

		List<Listexpert> all = service.findBy("status in (7,6) order by id desc",Listexpert.class);
		List<Listexpert> list = new ArrayList<>();
		for(int i=0;i<all.size();i++){
			String expert_ids = all.get(i).getExpert_ids();
			if(expert_ids !=null && expert_ids !=""){
				String[] ids = expert_ids.split(",");
				for(int j=0;j<ids.length;j++){
					if(userid.trim().equals(ids[j].trim())){
						list.add(all.get(i));
						break;
					}
				}
			}

		}
		renderJson(list);
	}
	public void listbyme(){
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

	public void save() {
		Listexpert listexpert = mapping(PERFIX, Listexpert.class);
		boolean temp = service.save(listexpert);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}
	/*
	修改项目状态值
	 */
	public void update() {
        Integer id = getParaToInt(0);
        Integer status = getParaToInt(1);

        Listexpert li = new Listexpert();
        li.setId(id);
        li.setStatus(status);
		boolean temp = service.update(li);
		if (temp) {
            setAttr("href","/listcom/index");
            setAttr("msg","秒后跳转到审核列表页面");
			render("/platform/projects/success.html");
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
